// app.js
import util from "/utils/util.js"
import Dialog from "@vant/weapp/dialog/dialog";
import Toast from "@vant/weapp/toast/toast";
const $api = require("/utils/http.js").API;
App({
  onLaunch() {
    let _this = this;
    // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || [];
    // logs.unshift(Date.now());
    // wx.setStorageSync('logs', logs);

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // console.log(res)
        _this.globalData.code = res.code;
      }
    });

    wx.getSystemInfo({
      success(res) {
        _this.globalData.bottomHeight = res.screenHeight - res.safeArea.bottom;
      }
    });

    // 新版本检测
    if (wx.canIUse("getUpdateManager")) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: "更新提示",
              content: "新版本已经准备好，是否重启应用？",
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            });
          });
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: "已经有新版本了哟~",
              content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~"
            });
          });
        }
      })
    } else {
      wx.showModal({
        title: "提示",
        content: "当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。"
      })
    }
  },
  globalData: {
    http: $api,
    code: "",
    util: util,
    vant: {
      toast: Toast,
      dialog: Dialog
    },
    bottomHeight: "",
    location: {},
    reg: {
      number: /^\d{1,}$/,
      realName: /^[\u4e00-\u9fa5]{2,4}$/,
      idCard: /^\d{6}((((((19|20)\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(((19|20)\d{2})(0[13578]|1[02])31)|((19|20)\d{2})02(0[1-9]|1\d|2[0-8])|((((19|20)([13579][26]|[2468][048]|0[48]))|(2000))0229))\d{3})|((((\d{2})(0[13-9]|1[012])(0[1-9]|[12]\d|30))|((\d{2})(0[13578]|1[02])31)|((\d{2})02(0[1-9]|1\d|2[0-8]))|(([13579][26]|[2468][048]|0[048])0229))\d{2}))(\d|X|x)$/,
      tel: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/,
      price: /(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/,
      wxid: /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/
    }
  }
})